import axios from "axios"
import constants from "@/common/constants"
import catchUtil from "@/utils/catchUtil"

//配置axios
const service = axios.create({
	baseURL:constants.HTTP_CONFIG.baseUrl,
	timeout:constants.HTTP_CONFIG.timeout,
	//其他配置
	
})

//设置拦截器
service.interceptors.request.use(config =>{
	//设置token
	const userInfo = catchUtil.getLocalStorage('userInfo')
	if(userInfo && userInfo.token){
		//设置token的权限
		config.headers.Authorization = userInfo.token
	}
	return config;
})

//设置响应拦截
service.interceptors.response.use(response =>{
	return response.data
},(error) => {
	console.log('我出错了')
	if(error && error.response){
		switch(error.response.satus){
			case 404:error.message = '您访问的页面不存治安';
			break;
			case 500:error.message = '页面访问超时，出错了';
			break;
		}
		if(error.message.includes('timeout')){
			error.message = '访问超时'
		}
		return Promise.reject(error.message)
	}
})

export default service